/*
 * Copyright 2012 The LoadTestAnalyzer Project
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package org.yantsu.loadtestanalyzer.ltaimporter;

import org.yantsu.loadtestanalyzer.ltaimporter.importers.Importer;

/**
 * Creates Importers for given agenttypes.
 * Importers are classes in the package org.yantsu.loadtestanalyzer.ltaimporter named as the agenttype.
 * First character of the name is uppercase.
 * Importer classes have to implement the interface Importer
 * 
 * @author cthiele
 *
 */
public class ImporterFactory {
	private static final String IMPORTER_PACKAGE = "org.yantsu.loadtestanalyzer.ltaimporter.importers";
	
	/**
	 * Create an importer for the given agenttype.
	 * 
	 * @param agenttype
	 * @return
	 */
	static public Importer createImporter(String agenttype) {
		String className = Character.toUpperCase(agenttype.charAt(0)) + agenttype.substring(1);
		try {
			Class<?> importerClass = Class.forName(IMPORTER_PACKAGE + "." + className);
			Importer importer = (Importer)importerClass.newInstance();
			return importer;
		} catch (ClassNotFoundException e) {
			throw new IllegalStateException("Class with name " + className + " not found");
		} catch (InstantiationException e) {
			throw new IllegalStateException("Class with name " + className + " cannot be instanciated", e);
		} catch (IllegalAccessException e) {
			throw new IllegalStateException("Class with name " + className + " cannot be instanciated", e);
		}
	}
}
